﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.InventoryCommands
{
    public class LiveStockSearchHandler : IRequestHandler<LiveStockSearchCommand, Result>
    {
        //栋舍
        private readonly IBaseRepository<Cottages> cottagebaseRepository;
        //栏位
        private readonly IBaseRepository<Fields> fieldsRepository;
        //羊场
        private readonly IBaseRepository<SheepFarm> farmbaseRepository;
        //品种
        private readonly IBaseRepository<Variety> VarietybaseRepository;
        //基因等级
        private readonly IBaseRepository<GeneGrade> geneGradeRepository;
        //羊只类型
        private readonly IBaseRepository<SleepType> sleeptypebaseRepository;

        //档案信息
        private readonly IBaseRepository<SleepInfor> sleepinforbaseRepository;
        //出生状态
        private readonly IBaseRepository<BirthStatus> birthstatubaseRepository;
        //栋舍类型
        private readonly IBaseRepository<CottageType> cottageTypeRepository;
        //种公种母后裔期初
        private readonly  IBaseRepository<FemaleBegin> femaleBeginRepository;
        private readonly IBaseRepository<MaleBegin> maleBeginRepository;
        private readonly IBaseRepository<ProBegin> proBeginRepository;



        public LiveStockSearchHandler(IBaseRepository<Cottages> cottagebaseRepository, IBaseRepository<Variety> varietybaseRepository,
                                        IBaseRepository<GeneGrade> geneGradeRepository, IBaseRepository<ProBegin> proBeginRepository,
                                        IBaseRepository<Fields> fieldsRepository, IBaseRepository<FemaleBegin> femaleBeginRepository,
                                        IBaseRepository<SleepInfor> sleepinforbaseRepository, IBaseRepository<MaleBegin> maleBeginRepository,
                                        IBaseRepository<BirthStatus> birthstatubaseRepository, IBaseRepository<SheepFarm> farmbaseRepository,
                                        IBaseRepository<SleepType> sleeptypebaseRepository, IBaseRepository<CottageType> cottageTypeRepository)

        {
            this.cottagebaseRepository = cottagebaseRepository;
            this.VarietybaseRepository = varietybaseRepository;
            this.geneGradeRepository = geneGradeRepository;
            this.fieldsRepository = fieldsRepository;
            this.sleepinforbaseRepository = sleepinforbaseRepository;
            this.birthstatubaseRepository = birthstatubaseRepository;
            this.farmbaseRepository = farmbaseRepository;
            this.sleeptypebaseRepository = sleeptypebaseRepository;
            this.cottageTypeRepository = cottageTypeRepository;
            this.proBeginRepository = proBeginRepository;
            this.femaleBeginRepository = femaleBeginRepository;
            this.maleBeginRepository = maleBeginRepository;
        }

        public async Task<Result> Handle(LiveStockSearchCommand request, CancellationToken cancellationToken)
        {
            var result = new Result() { Code = (int)CodeEnum.search };
            var res = (from a in sleepinforbaseRepository.GetAll()
                       join b in farmbaseRepository.GetAll() on a.FarmId equals b.FarmId
                       join c in cottagebaseRepository.GetAll() on a.RecordcottageId equals c.CottagesId
                       join d in fieldsRepository.GetAll() on a.FieIdId equals d.FieIdId
                       join e in sleeptypebaseRepository.GetAll() on a.SleepTypeId equals e.SleepTypeId
                       //join f in birthstatubaseRepository.GetAll() on a.BirthdayId equals f.BirthdayId
                       join g in VarietybaseRepository.GetAll() on a.VarietyId equals g.VarietyId
                       join h in geneGradeRepository.GetAll() on a.GradeId equals h.GradeId
                       join i in cottageTypeRepository.GetAll() on c.CottageTypeId equals i.CottageTypeId

                       select new SleepRecordDto()
                       {
                           SleepInforId = a.SleepInforId,
                           EarNumber = a.EarNumber,
                           VarietyId = a.VarietyId,
                           ElectronicEarTag = a.ElectronicEarTag,
                           GradeId = a.GradeId,
                           SheepImage = a.SheepImage,
                           Gender = a.Gender,
                           SleepTypeId = a.SleepTypeId,
                           StayStatus = a.StayStatus,
                           FarmId = a.FarmId,
                           RecordcottageId = a.RecordcottageId,
                           FieIdId = a.FieIdId,
                           FarEarNumber = a.FarEarNumber,
                           MoEarNumber = a.MoEarNumber,
                           SleepBirthDate = a.SleepBirthDate,
                           SleepEntryDate = a.SleepEntryDate,
                           SleepBirthPlace = a.SleepBirthPlace,
                           SleepBirthWeight = a.SleepBirthWeight,
                           SleepNotes = a.SleepNotes,
                           BirthdayId = a.BirthdayId,
                           SleepTypeName = e.SleepTypeName,
                           //BirsdayName = f.BirsdayName,
                           Grade = h.Grade,
                           CottageTypeId = i.CottageTypeId,
                           CottageName = c.CottageName,
                           FieIdName = d.FieIdName,
                           BreedStatuId = a.BreedStatuId,
                           BreedDayNum = a.BreedDayNum,
                           CheckPre = a.CheckPre,
                           ProductLevel = a.ProductLevel,
                           BuyLevel = a.BuyLevel,
                           WeanWeight = a.WeanWeight,
                           WeanData = a.WeanData,
                           //AgeDay = (DateTime.Now.Day - a.SleepBirthDate.Day)+ (DateTime.Now.Month - a.SleepBirthDate.Month)*30,
                           agetotal = (int)(DateTime.Now - Convert.ToDateTime(a.SleepBirthDate)).TotalDays,
                           VarietyName = g.VarietyName,
                           GetDateTime=DateTime.Now,
                       }).ToList();
            //当前羊只存栏总数
            var scount=res.Count;
            //找到当前时间的一周前
            DateTime startTime= DateTime.Now;
            DateTime endTime= startTime.AddDays(-7);
            //通过创建时间找到一周前的数量
            var Flist=femaleBeginRepository.GetAll().Where(x=>x.CreateTime<= endTime).ToList();
            var fcount=Flist.Count;
            var Mlist=maleBeginRepository.GetAll().Where(x => x.CreateTime <= endTime).ToList();
            var mcount=Mlist.Count;
            var Plist=proBeginRepository.GetAll().Where(x => x.CreateTime <= endTime).ToList();
            var pcount=Plist.Count;
            //找到一周前总数和现在的总数作比较
            var Sum=fcount+mcount+pcount;

            if (scount > Sum)
            {
                result.Percen = (scount - Sum)*1.0 / Sum * 100;
            }
            else
            {
                result.Percen=-(Sum-scount) * 1.0 / Sum* 100;
            }
          
            result.Count=res.Count;
            return result;
        }
    }
}
